<template>
  <el-card class="swiper-container">
    <template #header>
      <div class="header">
        <el-button
          type="primary"
          size="small"
          icon="el-icon-plus"
          @click="handleAdd"
          >增加</el-button
        >
        <el-popconfirm title="确定删除吗？" @confirm="handleDelete">
          <template #reference>
            <el-button type="danger" size="small" icon="el-icon-delete"
              >批量删除</el-button
            >
          </template>
        </el-popconfirm>
      </div>
    </template>
    <el-table
      v-loading="loading"
      ref="multipleTable"
      :data="tableData"
      tooltip-effect="dark"
      style="width: 100%"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column label="轮播图" width="200">
        <template #default="scope">
          <img
            style="width: 150px; height: 150px"
            :src="scope.row.carouselUrl"
            alt="轮播图"
          />
        </template>
      </el-table-column>
      <el-table-column label="跳转链接">
        <template #default="scope">
          <a target="_blank" :href="scope.row.redirectUrl">{{
            scope.row.redirectUrl
          }}</a>
        </template>
      </el-table-column>
      <el-table-column prop="carouselRank" label="排序值" width="120">
      </el-table-column>
      <el-table-column prop="createTime" label="添加时间" width="200">
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template #default="scope">
          <a
            style="cursor: pointer; margin-right: 10px"
            @click="handleEdit(scope.row.carouselId)"
            >修改</a
          >
          <el-popconfirm
            title="确定删除吗？"
            @confirm="handleDeleteOne(scope.row.carouselId)"
          >
            <template #reference>
              <a style="cursor: pointer">删除</a>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>
    <!--总数超过一页，再展示分页器-->
    <el-pagination
      background
      layout="prev, pager, next"
      :total="total"
      :page-size="pageSize"
      :current-page="currentPage"
      @current-change="changePage"
    />
  </el-card>
  <DialogAddSwiper ref="addGood" :reload="getCarousels" :type="type" />
</template>

<script>
import { onMounted, reactive, ref, toRefs } from "vue";
import { ElMessage } from "element-plus";
import DialogAddSwiper from "@/components/DialogAddSwiper.vue";
import axios from "@/utils/axios";
export default {
  name: "Swiper",
  components: {
    DialogAddSwiper,
  },
  setup() {
    const multipleTable = ref(null);
    const addGood = ref(null);
    const state = reactive({
      loading: false,
      tableData: [], // 数据列表
      multipleSelection: [], // 选中项
      total: 0, // 总条数
      currentPage: 1, // 当前页
      pageSize: 10, // 分页大小
      type: "add", // 操作类型
    });
    onMounted(() => {
      getCarousels();
    });
    // 获取轮播图列表
    const getCarousels = () => {
      state.loading = true;

      setTimeout(() => {
        const list = [
          {
            carouselId: 967,
            carouselUrl:
              "http://backend-api-02.newbee.ltd/upload/20220601_00005214.webp",
            redirectUrl:
              "http://img.daimg.com/uploads/allimg/200602/1-2006021JT8.jpg",
            carouselRank: 300,
            isDeleted: 0,
            createTime: "2022-03-24 09:55:24",
            createUser: 0,
            updateTime: "2022-06-01 00:00:55",
            updateUser: 0,
          },
          {
            carouselId: 963,
            carouselUrl:
              "http://backend-api-02.newbee.ltd/upload/20220601_01521055.jpg",
            redirectUrl:
              "http://baidu.com/wallpaper/2020-04-10/5e8fd7e7df2e8.jpg",
            carouselRank: 297,
            isDeleted: 0,
            createTime: "2022-03-23 17:37:25",
            createUser: 0,
            updateTime: "2022-06-01 01:52:17",
            updateUser: 0,
          },
          {
            carouselId: 966,
            carouselUrl:
              "http://backend-api-02.newbee.ltd/upload/20220514_12361362.jpg",
            redirectUrl:
              "https://baike.baidu.com/item/%E6%B3%95%E5%BC%8F%E7%83%A4%E7%BE%8A%E6%8E%92/11037981?fr=aladdin",
            carouselRank: 296,
            isDeleted: 0,
            createTime: "2022-03-24 09:35:54",
            createUser: 0,
            updateTime: "2022-05-31 17:22:02",
            updateUser: 0,
          },
          {
            carouselId: 1040,
            carouselUrl:
              "http://backend-api-02.newbee.ltd/upload/20220519_15480818.jpg",
            redirectUrl: "焦糖布丁",
            carouselRank: 295,
            isDeleted: 0,
            createTime: "2022-04-06 16:38:53",
            createUser: 0,
            updateTime: "2022-05-31 17:44:21",
            updateUser: 0,
          },
          {
            carouselId: 969,
            carouselUrl:
              "http://backend-api-02.newbee.ltd/upload/20220519_1539144.jpg",
            redirectUrl:
              "https://baike.baidu.com/item/%E9%BA%BB%E8%BE%A3%E5%B0%8F%E9%BE%99%E8%99%BE/2149989?fr=aladdin",
            carouselRank: 294,
            isDeleted: 0,
            createTime: "2022-03-24 16:36:46",
            createUser: 0,
            updateTime: "2022-05-19 15:48:32",
            updateUser: 0,
          },
          {
            carouselId: 1042,
            carouselUrl:
              "http://backend-api-02.newbee.ltd/upload/20220519_15484550.jpg",
            redirectUrl: "面包",
            carouselRank: 292,
            isDeleted: 0,
            createTime: "2022-04-06 16:39:15",
            createUser: 0,
            updateTime: "2022-05-19 15:48:56",
            updateUser: 0,
          },
          {
            carouselId: 1041,
            carouselUrl:
              "http://backend-api-02.newbee.ltd/upload/20220519_15490034.jpg",
            redirectUrl: "www.baidu.com",
            carouselRank: 291,
            isDeleted: 0,
            createTime: "2022-04-06 16:39:07",
            createUser: 0,
            updateTime: "2022-05-22 14:59:55",
            updateUser: 0,
          },
          {
            carouselId: 1055,
            carouselUrl:
              "http://backend-api-02.newbee.ltd/upload/20220519_15504795.jpg",
            redirectUrl: "披萨",
            carouselRank: 291,
            isDeleted: 0,
            createTime: "2022-04-06 16:42:47",
            createUser: 0,
            updateTime: "2022-05-19 15:51:09",
            updateUser: 0,
          },
          {
            carouselId: 978,
            carouselUrl:
              "http://backend-api-02.newbee.ltd/upload/20220521_22204930.jpg",
            redirectUrl: "www.baidu.com",
            carouselRank: 291,
            isDeleted: 0,
            createTime: "2022-03-25 18:41:26",
            createUser: 0,
            updateTime: "2022-05-31 21:55:19",
            updateUser: 0,
          },
          {
            carouselId: 1054,
            carouselUrl:
              "http://backend-api-02.newbee.ltd/upload/20220519_15505643.jpg",
            redirectUrl: "牛排",
            carouselRank: 290,
            isDeleted: 0,
            createTime: "2022-04-06 16:42:42",
            createUser: 0,
            updateTime: "2022-05-19 15:51:49",
            updateUser: 0,
          },
        ];
        state.tableData = list;
        state.total = 1442;
        state.currentPage = 145;
        state.loading = false;
      }, 2000);

    };
    // 添加轮播项
    const handleAdd = () => {
      state.type = "add";
      addGood.value.open();
    };
    // 修改轮播图
    const handleEdit = (id) => {
      state.type = "edit";
      addGood.value.open(id);
    };
    // 选择项
    const handleSelectionChange = (val) => {
      state.multipleSelection = val;
    };
    // 批量删除
    const handleDelete = () => {
      if (!state.multipleSelection.length) {
        ElMessage.error("请选择项");
        return;
      }
      axios
        .delete("/carousels", {
          data: {
            ids: state.multipleSelection.map((i) => i.carouselId),
          },
        })
        .then(() => {
          ElMessage.success("删除成功");
          getCarousels();
        });
    };
    // 单个删除
    const handleDeleteOne = (id) => {
      axios
        .delete("/carousels", {
          data: {
            ids: [id],
          },
        })
        .then(() => {
          ElMessage.success("删除成功");
          getCarousels();
        });
    };
    const changePage = (val) => {
      state.currentPage = val;
      getCarousels();
    };
    return {
      ...toRefs(state),
      multipleTable,
      handleSelectionChange,
      addGood,
      handleAdd,
      handleEdit,
      handleDelete,
      handleDeleteOne,
      getCarousels,
      changePage,
    };
  },
};
</script>

<style scoped>
.swiper-container {
  min-height: 100%;
}
.el-card.is-always-shadow {
  min-height: 100% !important;
}
</style>